using System;
using System.Collections.Generic;
using LitJson;
namespace CGF
{
    public partial class PanelDeviceIncome : UIComponentBase, IAwake<UILayer, bool>, IDestroy
    {
        public static void Open()
        {
            UIManagerComponent.instance.OpenUI<PanelDeviceIncome>(UILayer.Normal, RootComponent.instance, false);
        }

        public static void Close()
        {
            UIManagerComponent.instance.CloseUI<PanelDeviceIncome>();
        }

        public async void Awake(UILayer layer, bool isFloat)
        {
            await SpawnPrefab("Prefabs/GameUI/PanelDeviceIncome", layer, isFloat);
            BindProperties(transform);

            BtnBack.SetClick(Close);

            SetTotalAmount("0");
            ReqData();
        }

        private async void ReqData()
        {
            //总收益
            var res = await Network.Get("/v1/user/wallet");
            JsonData wallet = res.jsonData["data"]["wallet"];
            int totalIncome = (int)wallet["total_income"];
            SetTotalAmount(StringHelper.Format2Decimal(totalIncome * 0.01f));
            //图表
            res = await Network.Get($"/v1/earnings/dash");
            JsonData data = res.jsonData["data"];
            JsonData earnings = data["earnings"];
            string[] xNames = new string[7];
            float[] yValues = new float[7];
            for (int i = 0; i < xNames.Length; i++)
            {
                DateTime dateTime = DateTime.Now.AddDays(-xNames.Length + i);
                xNames[i] = dateTime.ToString(I18N.MonthDayFormat);
                yValues[i] = 0;
            }
            int dataIndex = earnings.Count - 1;
            for (int i = xNames.Length - earnings.Count; i < xNames.Length; i++)
            {
                JsonData earning = earnings[dataIndex];
                xNames[i] = TimeHelper.Parse2Date(earning["date"].ToString(), false);
                string value = StringHelper.Format2Decimal((int)earning["income"] * 0.01f);
                yValues[i] = float.Parse(value);
                dataIndex--;
            }
            Cart.GetComponent<Cart>().SetData(xNames, 0, 0, yValues);

            //表格
            string api = "/v1/earnings";
            string listKey = "Results";
            Table.Column[] columns =
            {
                (cell, data) => TimeHelper.Parse2Date(data["date"].ToString(), false),
                (cell, data) => $"<color={Config.NumColor}>{I18N.Currency}<b>{StringHelper.Format2Decimal((int)data["income"] * 0.01f)}</b></color>",
                (cell, data) => CommAssets.instance.I18N_detail,
            };
            Table.GetComponent<Table>().ReqData(api, listKey, columns, null, null, data =>
            {
                //2025-10-10T00:00:00+08:00
                string dateStr = data["date"].ToString();
                string[] arr = dateStr.Split('T');
                string[] arr2 = arr[0].Split('-');
                PanelDeviceIncomeDetail.Open(arr2[0],arr2[1],arr2[2]);
            });
        }

        private void SetTotalAmount(string amount)
        {
            TextTotal.text = $"{I18N_total_income}: <color={Config.NumColor}>{I18N.Currency} <b>{amount}</b></color>";
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
        }

    }
}
